import 'dart:convert';

import 'package:common_utils/common_utils.dart';
import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/AffairListModel.dart';
import 'package:du_application/provide/AuthProvider.dart';
import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AffairListProvider with ChangeNotifier {
   AffairListModel affairListModel;
   List<Affairs> affairs;
  
     Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  getAffair(context,page,limit) async {
     return await _prefs.then((ins) async{
    Options options =  Options(Provide.value<AuthProvider>(context).token);
    return await HttpUtil.getInstance().get("/affair/list",data: {
      "page":page,
      "limit":limit,
    },options:options.options).then((data) {
      
      affairListModel=AffairListModel.fromJson(json.decode(data));
      if(affairListModel.data.page==1){
            affairs=affairListModel.data.affairs;
        }else{
          if(affairs!=null){
   affairs.addAll(affairListModel.data.affairs);
          }
       
        }
        notifyListeners();
       return affairListModel;
     });
    });
  }
}